package com.xlh.dto.courseTest;

import com.google.common.collect.Lists;
import com.xlh.enums.courseTest.QuestionTypeEnum;
import com.xlh.util.BeanUtil;
import lombok.*;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;

/**
 * @author zhr 2019/3/22
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class AnswerSheetTypeDTO {
    // 题目类型
    private Integer type;
    // 题目类型说明
    private String typeName;
    // 题型下答题卡列表
    private List<AnswerSheetDTO> answerSheetList;
    // 题型得分,初始化分数为0
    private Integer score = 0;

    public AnswerSheetTypeDTO(Integer type, String typeName, List<AnswerSheetDTO> answerSheetTypeDTOList) {
        this.type = type;
        this.typeName = typeName;
        this.answerSheetList = answerSheetTypeDTOList;
    }

    public static List<AnswerSheetTypeDTO> transform(List<AnswerSheetDTO> answerSheetList) {
        List<AnswerSheetTypeDTO> result = Lists.newArrayList();
        Map<Integer, List<AnswerSheetDTO>> typeMap = BeanUtil.aggByKeyToList("type", answerSheetList);
        typeMap.forEach((type, list) -> {
            int score = 0;
            String typeMessage = null;
            if (CollectionUtils.isNotEmpty(list)) {
                score = list.stream().map(AnswerSheetDTO::getScore).reduce(0, Integer::sum);
            }
            QuestionTypeEnum typeEnum = QuestionTypeEnum.forCode(type);
            if (typeEnum != null) typeMessage = typeEnum.getMessage();
            result.add(new AnswerSheetTypeDTO(type, typeMessage, list, score));
        });
        return result;
    }
}



